﻿using System;
using Rewired;
using UnityEngine;

public class JoystickListener : MonoBehaviour {

    private void OnEnable()
    {
        if (!ReInput.isReady)
        {
            Debug.Log("!ReInput.isReady");
            return;
        }

        Player player = ReInput.players.GetPlayer(0);
        if (player == null)
        {
            Debug.Log("player == null");
            return;
        }

        player.AddInputEventDelegate(OnHorizontal, UpdateLoopType.Update, InputActionEventType.AxisActive, "Horizontal");
        player.AddInputEventDelegate(OnHorizontalInactive, UpdateLoopType.Update, InputActionEventType.AxisInactive, "Horizontal");
        player.AddInputEventDelegate(OnVertical, UpdateLoopType.Update, InputActionEventType.AxisActive, "Vertical");
        player.AddInputEventDelegate(OnVerticalInactive, UpdateLoopType.Update, InputActionEventType.AxisInactive, "Vertical");
        player.AddInputEventDelegate(OnScrollWheel, UpdateLoopType.Update, InputActionEventType.AxisActive, "ScrollWheel");
        player.AddInputEventDelegate(OnScrollWheelInactive, UpdateLoopType.Update, InputActionEventType.AxisInactive, "ScrollWheel");

        InputActionEventType pressType = InputActionEventType.ButtonJustPressed;
        player.AddInputEventDelegate(OnMouseButton0, UpdateLoopType.Update, pressType, "MouseButton0");
        player.AddInputEventDelegate(OnMouseButton1, UpdateLoopType.Update, pressType, "MouseButton1");
        player.AddInputEventDelegate(OnMouseButton2, UpdateLoopType.Update, pressType, "MouseButton2");
        player.AddInputEventDelegate(OnTabKey, UpdateLoopType.Update, pressType, "TabKey");
        player.AddInputEventDelegate(OnAltKey, UpdateLoopType.Update, pressType, "AltKey");
        player.AddInputEventDelegate(OnReturnKey, UpdateLoopType.Update, pressType, "ReturnKey");
        player.AddInputEventDelegate(OnSpaceKey, UpdateLoopType.Update, pressType, "SpaceKey");
        player.AddInputEventDelegate(OnEscapeKey, UpdateLoopType.Update, pressType, "EscapeKey");
        player.AddInputEventDelegate(OnLeftKey, UpdateLoopType.Update, pressType, "LeftKey");
        player.AddInputEventDelegate(OnRightKey, UpdateLoopType.Update, pressType, "RightKey");
        player.AddInputEventDelegate(OnUpKey, UpdateLoopType.Update, pressType, "UpKey");
        player.AddInputEventDelegate(OnDownKey, UpdateLoopType.Update, pressType, "DownKey");

        InputActionEventType unpressType = InputActionEventType.ButtonJustReleased;
        player.AddInputEventDelegate(OnMouseButton0Released, UpdateLoopType.Update, unpressType, "MouseButton0");
        player.AddInputEventDelegate(OnMouseButton1Released, UpdateLoopType.Update, unpressType, "MouseButton1");
        player.AddInputEventDelegate(OnMouseButton2Released, UpdateLoopType.Update, unpressType, "MouseButton2");
        player.AddInputEventDelegate(OnTabKeyReleased, UpdateLoopType.Update, unpressType, "TabKey");
        player.AddInputEventDelegate(OnAltKeyReleased, UpdateLoopType.Update, unpressType, "AltKey");
        player.AddInputEventDelegate(OnReturnKeyReleased, UpdateLoopType.Update, unpressType, "ReturnKey");
        player.AddInputEventDelegate(OnSpaceKeyReleased, UpdateLoopType.Update, unpressType, "SpaceKey");
        player.AddInputEventDelegate(OnEscapeKeyReleased, UpdateLoopType.Update, unpressType, "EscapeKey");
        player.AddInputEventDelegate(OnLeftKeyReleased, UpdateLoopType.Update, unpressType, "LeftKey");
        player.AddInputEventDelegate(OnRightKeyReleased, UpdateLoopType.Update, unpressType, "RightKey");
        player.AddInputEventDelegate(OnUpKeyReleased, UpdateLoopType.Update, unpressType, "UpKey");
        player.AddInputEventDelegate(OnDownKeyReleased, UpdateLoopType.Update, unpressType, "DownKey");
    }

    private void OnScrollWheelInactive(InputActionEventData obj)
    {
        //Debug.Log("OnScrollWheelInactive");
    }

    private void OnScrollWheel(InputActionEventData obj)
    {
        //Debug.Log("OnScrollWheel");
    }

    private void OnVerticalInactive(InputActionEventData obj)
    {
        //Debug.Log("OnVerticalInactive");
    }

    private void OnHorizontalInactive(InputActionEventData obj)
    {
        //Debug.Log("OnHorizontalInactive");
    }

    private void OnVertical(InputActionEventData obj)
    {
        //Debug.Log("OnVertical");
    }

    private void OnHorizontal(InputActionEventData obj)
    {
        //Debug.Log("OnHorizontal");
    }

    private void OnAltKey(InputActionEventData obj)
    {
        //Debug.Log("OnAltKey");
        KeyboardSimulation.KeyDown(KeyboardSimulation.VK_MENU);
    }

    private void OnAltKeyReleased(InputActionEventData obj)
    {
        //Debug.Log("OnAltKeyReleased");
        KeyboardSimulation.KeyUp(KeyboardSimulation.VK_MENU);
    }

    private void OnDownKey(InputActionEventData obj)
    {
        //Debug.Log("OnDownKey");
        KeyboardSimulation.KeyDown(KeyboardSimulation.VK_DOWN);
    }

    private void OnDownKeyReleased(InputActionEventData obj)
    {
        //Debug.Log("OnDownKeyReleased");
        KeyboardSimulation.KeyUp(KeyboardSimulation.VK_DOWN);
    }

    private void OnUpKey(InputActionEventData obj)
    {
        //Debug.Log("OnUpKey");
        KeyboardSimulation.KeyDown(KeyboardSimulation.VK_UP);
    }

    private void OnUpKeyReleased(InputActionEventData obj)
    {
        //Debug.Log("OnUpKeyReleased");
        KeyboardSimulation.KeyUp(KeyboardSimulation.VK_UP);
    }

    private void OnRightKey(InputActionEventData obj)
    {
        //Debug.Log("OnRightKey");
        KeyboardSimulation.KeyDown(KeyboardSimulation.VK_RIGHT);
    }

    private void OnRightKeyReleased(InputActionEventData obj)
    {
        //Debug.Log("OnRightKeyReleased");
        KeyboardSimulation.KeyUp(KeyboardSimulation.VK_RIGHT);
    }

    private void OnLeftKey(InputActionEventData obj)
    {
        //Debug.Log("OnLeftKey");
        KeyboardSimulation.KeyDown(KeyboardSimulation.VK_LEFT);
    }

    private void OnLeftKeyReleased(InputActionEventData obj)
    {
        //Debug.Log("OnLeftKeyReleased");
        KeyboardSimulation.KeyUp(KeyboardSimulation.VK_LEFT);
    }


    private void OnEscapeKey(InputActionEventData obj)
    {
        //Debug.Log("OnEscapeKey");
        KeyboardSimulation.KeyDown(KeyboardSimulation.VK_ESCAPE);
    }

    private void OnEscapeKeyReleased(InputActionEventData obj)
    {
        //Debug.Log("OnEscapeKeyReleased");
        KeyboardSimulation.KeyUp(KeyboardSimulation.VK_ESCAPE);
    }

    private void OnSpaceKeyReleased(InputActionEventData obj)
    {
        Debug.Log("OnSpaceKeyReleased");
        KeyboardSimulation.KeyUp(KeyboardSimulation.VK_SPACE);
    }

    private void OnSpaceKey(InputActionEventData obj)
    {
        Debug.Log("OnSpaceKey");
        KeyboardSimulation.KeyDown(KeyboardSimulation.VK_SPACE);
    }

    private void OnMouseButton2Released(InputActionEventData obj)
    {
        Debug.Log("OnMouseButton2Released");
        MouseSimulation.MouseButton2Up();
    }

    private void OnMouseButton1Released(InputActionEventData obj)
    {
        Debug.Log("OnMouseButton1Released");
        MouseSimulation.MouseButton1Up();
    }

    private void OnMouseButton0Released(InputActionEventData obj)
    {
        Debug.Log("OnMouseButton0Released");
        MouseSimulation.MouseButton0Up();
    }

    private void OnMouseButton2(InputActionEventData obj)
    {
        Debug.Log("OnMouseButton2");
        MouseSimulation.MouseButton2Down();
    }

    private void OnMouseButton1(InputActionEventData obj)
    {
        Debug.Log("OnMouseButton1");
        MouseSimulation.MouseButton1Down();
    }

    private void OnMouseButton0(InputActionEventData obj)
    {
        Debug.Log("OnMouseButton0");
        MouseSimulation.MouseButton0Down();
    }

    private void OnReturnKey(InputActionEventData obj)
    {
        //Debug.Log("OnReturnKey");
        KeyboardSimulation.KeyDown(KeyboardSimulation.VK_RETURN);
    }

    private void OnReturnKeyReleased(InputActionEventData obj)
    {
        //Debug.Log("OnReturnKeyReleased");
        KeyboardSimulation.KeyUp(KeyboardSimulation.VK_RETURN);
    }

    private void OnTabKeyReleased(InputActionEventData obj)
    {
        Debug.Log("OnTabKeyReleased");
        KeyboardSimulation.KeyUp(KeyboardSimulation.VK_TAB);
    }

    private void OnTabKey(InputActionEventData obj)
    {
        Debug.Log("OnTabKey");
        KeyboardSimulation.KeyDown(KeyboardSimulation.VK_TAB);
    }

    private void OnDisable()
    {
        if (!ReInput.isReady) return;

        Player player = ReInput.players.GetPlayer(0);
        if (player == null) return;

        player.RemoveInputEventDelegate(OnHorizontal);
        player.RemoveInputEventDelegate(OnVertical);

        player.RemoveInputEventDelegate(OnMouseButton0);
        player.RemoveInputEventDelegate(OnMouseButton1);
        player.RemoveInputEventDelegate(OnMouseButton2);
        player.RemoveInputEventDelegate(OnTabKey);
        player.RemoveInputEventDelegate(OnReturnKey);
        player.RemoveInputEventDelegate(OnSpaceKey);
        player.RemoveInputEventDelegate(OnEscapeKey);
        player.RemoveInputEventDelegate(OnLeftKey);
        player.RemoveInputEventDelegate(OnRightKey);
        player.RemoveInputEventDelegate(OnUpKey);
        player.RemoveInputEventDelegate(OnDownKey);

        player.RemoveInputEventDelegate(OnMouseButton0Released);
        player.RemoveInputEventDelegate(OnMouseButton1Released);
        player.RemoveInputEventDelegate(OnMouseButton2Released);
        player.RemoveInputEventDelegate(OnTabKeyReleased);
        player.RemoveInputEventDelegate(OnReturnKeyReleased);
        player.RemoveInputEventDelegate(OnSpaceKeyReleased);
        player.RemoveInputEventDelegate(OnEscapeKeyReleased);
        player.RemoveInputEventDelegate(OnLeftKeyReleased);
        player.RemoveInputEventDelegate(OnRightKeyReleased);
        player.RemoveInputEventDelegate(OnUpKeyReleased);
        player.RemoveInputEventDelegate(OnDownKeyReleased);
    }
}
